package zad1;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

class Buffor {

	ArrayList<String> datas;
	ArrayList<String> ids;
	int max;
	
	public Buffor(int size) {
		max = size;

		ids = new ArrayList<String>();
		datas = new ArrayList<String>();
		for (int i = 0; i < max; i++) {
			ids.add(null);
			datas.add(null);
		}
	}
	
	public synchronized void consume(Workers worker, int i) {

		while (ids.get(i) != worker.getWorkId())
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		try {
			TimeUnit.MILLISECONDS.sleep(300);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}

		datas.set(i, worker.consume(datas.get(i)));
		ids.set(i, worker.next());

		notifyAll();

	}

	public int next(int i) {
		return (i + 1) % max;
	}



}